package main

import (
	"context"
	"g_group-buy-market/internal/pkg/config"
	"g_group-buy-market/internal/pkg/middleware"
	"g_group-buy-market/internal/pkg/zaplog"
	"g_group-buy-market/pkg/globalService"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log"
	"net/http"
	"os"
)

func main() {
	// 声明gin路由
	config, err := config.LoadConfig()
	if err != nil {
		panic("Error loading config：" + err.Error())
	}
	// zap日志初始化
	ctx := context.Background()
	logClient, err := zaplog.NewDevelopmentZapLogClient(ctx, config.ZapLog)
	if err != nil {
		log.Println("Error initializing zap logger: ", zap.Error(err))
		os.Exit(1) // 退出程序
	}
	// 设置全局日志变量
	globalService.ZapLogClient = logClient.GetLogger()
	log.Println("the service " + config.ServiceName + " is succeed, port" + config.Port)
	defer logClient.Close()
	// 日志和恢复（无崩溃）中间件
	router := gin.Default()
	reqMiddleWareConfig := middleware.HttpLogConfig(config, globalService.ZapLogClient)
	router.Use(middleware.HttpRequestTracerLogger(reqMiddleWareConfig))
	// 路由
	router.POST("/login", func(c *gin.Context) {
		c.JSON(200, gin.H{"message": "login"})
	})
	// 声明端口号
	http.ListenAndServe(config.Port, router)

}
